﻿using FrameServer.Net;
using System;
using System.Collections.Generic;

namespace FrameServer.Handler
{
    class NetMsgHandler : Singleton<NetMsgHandler>
    {
        private Dictionary<NetMsgID, Action<byte[], TCPClientPeer>> handlerMap;

        public void Init()
        {
            handlerMap = new Dictionary<NetMsgID, Action<byte[], TCPClientPeer>>();
            var commonHandler = new CommonHandler();
            var roomHandler = new RoomHandler();
        }

        public void RegisterReq(NetMsgID msgId, Action<byte[], TCPClientPeer> handler)
        {
            if (handlerMap.ContainsKey(msgId))
            {
                LogUtil.Log($"{msgId}重复注册", LogLevel.WARN);
                return;
            }
            handlerMap.Add(msgId, handler);
        }

        public void HandleReq(NetMsgID msgId, byte[] data, TCPClientPeer clientPeer)
        {
            if (!handlerMap.ContainsKey(msgId))
            {
                LogUtil.Log($"未注册的消息：{msgId}", LogLevel.WARN);
                return;
            }
            handlerMap[msgId].Invoke(data, clientPeer);
        }
    }
}
